package com.wuming.house.service.impl;

import com.wuming.house.core.BusinessException;
import com.wuming.house.core.HouseContant;
import com.wuming.house.dao.CateHouseMapper;
import com.wuming.house.model.CateHouse;
import com.wuming.house.service.CateHouseService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("cateHouseService")
public class CateHouseServiceImpl extends BaseServiceImpl implements CateHouseService {

    @Autowired
    private CateHouseMapper cateHouseMapper;

    @Override
    public void saveCateHouse(CateHouse ch) throws BusinessException {
        cateHouseMapper.insert(ch);
    }

    @Override
    public List<CateHouse> getHouseByParams(Integer cateId, String houseName, String area, String price, String houseLayout, int pageIndex, int pageSize) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("cateId", cateId);
        if (StringUtils.isNotBlank(houseName)) {
            params.put("houseName", houseName);
        }
        if (StringUtils.isNotBlank(area) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(area) && !HouseContant.DEFAULT_AREA_DESC.equalsIgnoreCase(area)) {
            params.put("area", area);
        }
        if (StringUtils.isNotBlank(price) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(price) && !HouseContant.DEFAULT_PRICE_DESC.equalsIgnoreCase(price)) {
            if (StringUtils.startsWith(price, "<")) {
                String endPrice = StringUtils.substring(price, 1);
                params.put("endPrice", endPrice);
            } else if (StringUtils.startsWith(price, ">")) {
                String startPrice = StringUtils.substring(price, 1);
                params.put("startPrice", startPrice);
            } else {
                String[] priceArr = StringUtils.split(price, "-");
                params.put("startPrice", priceArr[0]);
                params.put("endPrice", priceArr[1]);
            }
        }
        if (StringUtils.isNotBlank(houseLayout) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(houseLayout) && !HouseContant.DEFAULT_LAYOUT_DESC.equalsIgnoreCase(houseLayout)) {
            params.put("houseLayout", houseLayout);
        }
        try {
            return (List<CateHouse>) this.getPageList(CateHouseMapper.class, "selectHouseByCate", params, pageIndex, pageSize);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public int getTotal(Integer cateId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("cateId", cateId);
        try {
            return this.getTotal(CateHouseMapper.class, "selectTotal", params);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    @Override
    public CateHouse getHouseCateByCateIdAndHouseId(Long cateId, Long houseId) throws BusinessException {
        Map<String, Object> params = new HashMap<>();
        params.put("cateId", cateId);
        params.put("houseId", houseId);
        try {
            return cateHouseMapper.selectCateHouseByParams(params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public void deleteByHouseId(Long houseId) throws BusinessException {
        try {
            cateHouseMapper.deleteByParams(houseId);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteByParams(Long houseId, Integer cateId) {
        Map<String,Object> params = new HashMap<>();
        params.put("houseId",houseId);
        params.put("cateId",cateId);
        try {
            cateHouseMapper.deleteCateHouse(params);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public int getHouseTotal(Integer cateId, String houseName, String area, String price, String houseLayout) {
        Map<String, Object> params = new HashMap<>();
        params.put("cateId", cateId);
        if (StringUtils.isNotBlank(houseName)) {
            params.put("houseName", houseName);
        }
        if (StringUtils.isNotBlank(area) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(area) && !HouseContant.DEFAULT_AREA_DESC.equalsIgnoreCase(area)) {
            params.put("area", area);
        }
        if (StringUtils.isNotBlank(price) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(price) && !HouseContant.DEFAULT_PRICE_DESC.equalsIgnoreCase(price)) {
            if (StringUtils.startsWith(price, "<")) {
                String endPrice = StringUtils.substring(price, 1);
                params.put("endPrice", endPrice);
            } else if (StringUtils.startsWith(price, ">")) {
                String startPrice = StringUtils.substring(price, 1);
                params.put("startPrice", startPrice);
            } else {
                String[] priceArr = StringUtils.split(price, "-");
                params.put("startPrice", priceArr[0]);
                params.put("endPrice", priceArr[1]);
            }
        }
        if (StringUtils.isNotBlank(houseLayout) && !HouseContant.ALL_DEFAULT_SELECT.equalsIgnoreCase(houseLayout) && !HouseContant.DEFAULT_LAYOUT_DESC.equalsIgnoreCase(houseLayout)) {
            params.put("houseLayout", houseLayout);
        }
        try {
            return this.getTotal(CateHouseMapper.class, "selectHouseTotal", params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }
}
